package com.wego.controller;

import com.wego.common.alipay.RandomOrderNoUtil;
import com.wego.entity.domain.Goods;
import com.wego.entity.domain.Order;
import com.wego.entity.domain.OrderItem;
import com.wego.entity.domain.User;
import com.wego.entity.vo.CartItemVO;
import com.wego.service.GoodsService;
import com.wego.service.OrderItemService;
import com.wego.service.OrderService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: BuyGoodsController
 * @description: TODO 类描述
 * @author: Li Chuanwei
 * @date: 2023/07/17 16:35
 * @Company: Copyright [日期] by [作者或个人]
 **/

@Controller
@RequestMapping("/user/order")
public class BuyGoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @GetMapping("/buyGoods")
    public String buyOrder(String goodsIds, String Num, Model model, HttpSession session) {
        User user = (User)session.getAttribute("user");
        BigDecimal sum = new BigDecimal(0);
        int num = 0;
        String[] strArray = goodsIds.split(",");

        int[] aGoodsIds = new int[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            aGoodsIds[i] = Integer.parseInt(strArray[i]);
        }

        strArray = Num.split(",");

        int[] aNum = new int[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            aNum[i] = Integer.parseInt(strArray[i]);
            num += aNum[i];
        }

        Long orderId = Long.parseLong(RandomOrderNoUtil.createOrderId());
        Order order = new Order();
        order.setId(orderId);
        order.setUpdateTime(LocalDateTime.now());
        order.setCreateTime(LocalDateTime.now());
        order.setPayMethodId(2);
        order.setAmount(num);
        order.setState(1);
        order.setAddressId(1L);
        order.setUserId(user.getId());
        orderService.insertSelective(order);

        List<CartItemVO> cartItemVOList = new ArrayList<CartItemVO>();
        for (int i = 0; i < aGoodsIds.length; i++) {
            CartItemVO cartItemVO = new CartItemVO();
            final Goods goods = goodsService.selectByPrimaryKey(aGoodsIds[i]);
            cartItemVO.setName(goods.getName());
            cartItemVO.setPic(goods.getPic());
            cartItemVO.setPrice2(goods.getPrice2());
            cartItemVO.setStorage(aNum[i]);
            cartItemVOList.add(cartItemVO);
            BigDecimal itemPrice = new BigDecimal(aNum[i]).multiply(goods.getPrice2()); // 计算每一项的价格

            sum = sum.add(itemPrice); // 使用add方法进行累加
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(orderId);
            orderItem.setMoney(itemPrice);
            orderItem.setUpdateTime(LocalDateTime.now());
            orderItem.setCreateTime(LocalDateTime.now());
            orderItem.setState(1);
            orderItem.setGoodsId(goods.getId());
            orderItem.setAmount(aNum[i]);
            orderItemService.insertSelective(orderItem);
        }
        sum = sum.setScale(2, RoundingMode.HALF_UP);

        order.setMoney(sum);
        orderService.updateByPrimaryKeySelective(order);

        model.addAttribute("cartItemVOList",cartItemVOList);
        model.addAttribute("sum",sum.toString());
        model.addAttribute("orderNo", orderId);
        return "user/order_verify";
    }

    @GetMapping("/pay")
    public String payOrder (String orderNo,Model model){
        Order order = orderService.selectByPrimaryKey(orderNo);

        String orderAmt = String.valueOf(order.getMoney());
        int num = order.getAmount();
        model.addAttribute("orderAmt",orderAmt);
        model.addAttribute("orderSubject",String.valueOf(num) + "件商品");
        model.addAttribute("orderNo", orderNo);
        model.addAttribute("businessDesc","并夕夕电商平台");
        return "user/order_pay";
    }

}
